package com.ihome.android.market2.asynctask;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import com.ihome.android.EAApp;
import com.ihome.android.analytic.JsonAnalytic;
import com.ihome.android.http.HttpActions;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.academy.R;
import com.ihome.android.market2.frag.RecommAppFragment;
import com.ihome.android.market2.info.PosterInfo;
import com.ihome.android.market2.view.DialogFactory;
import com.ihome.android.market2.view.MyProgressDialog;
import com.ihome.android.util.exception.ITVException;
import com.ihome.android.util.log.LogUtil;

public class GetPosterTask extends AsyncTask<Bundle, Integer, Bundle> {
	private final String		TAG	= GetPosterTask.class.getSimpleName();
	private Context				context;
	private MyProgressDialog	progressDialog;
	private Fragment			fragment;

	private List<PosterInfo>	posterList;

	public GetPosterTask(Context context, Fragment fragment) {
		this.context = context;
		this.fragment = fragment;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();

		if (!(context instanceof Activity))
			return;
		progressDialog = MyProgressDialog.getDialog(context, context.getString(R.string.user_grabing_data));
		progressDialog.show();
		progressDialog.setCancelable(true);
		progressDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				if (GetPosterTask.this.getStatus().compareTo(AsyncTask.Status.FINISHED) != 0)
					GetPosterTask.this.cancel(true);
			}
		});
	}

	@Override
	protected Bundle doInBackground(Bundle... params) {
		Bundle b = params[0];

		try {
			// String result = Util.getTestDataFromAssets(context, "poster.json");
			String result = new HttpActions(context).getPoster();

			LogUtil.v(TAG, "result = " + result);
			if (!TextUtils.isEmpty(result)) {
				posterList = JsonAnalytic.getInstance().anslysePosterInfo(result);
			}
			else {
				cancel(true);
			}
		} catch (ITVException e) {
			e.printStackTrace();
			LogUtil.e(TAG, e.toString());
			b.putInt(HttpKeys.KEY_CODE, e.getStatusCode());
			b.putString(HttpKeys.KEY_DESCRIPTION, e.getMessage());
		}
		return b;
	}

	@Override
	protected void onPostExecute(Bundle result) {
		super.onPostExecute(result);
		if (progressDialog != null)
			progressDialog.dismiss();

		/** 请求出错处理 */
		if (result.containsKey(HttpKeys.KEY_CODE)) {
			if(ITVException.CODE_LOGINOUT_THREE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGINOUT_ONE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_SESSIONKEY == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGIN_ONE == result.getInt(HttpKeys.KEY_CODE)){
				new DialogFactory(context).showDialog(result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")");
				EAApp.getApp().exitApp(true);
				return;
			}
			new DialogFactory(context).showDialog(result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")");
			return;
		}

//		if (posterList != null && fragment instanceof RecommAppFragment) {
//			((RecommAppFragment) fragment).setData(posterList);
//		}

	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
		LogUtil.i(TAG, "--> onCancelled");
		if (progressDialog != null)
			progressDialog.dismiss();

	}

}
